package com.nari.sink.action;

import java.io.File;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.json.JSONUtil;
import org.apache.struts2.json.annotations.JSON;

import com.nari.baseinfo.model.IBuilding;
import com.nari.baseinfo.model.ICommunity;
import com.nari.baseinfo.model.IFileMapping;
import com.nari.baseinfo.model.IHeatRoom;
import com.nari.baseinfo.model.IProject;
import com.nari.baseinfo.service.IBuildingService;
import com.nari.baseinfo.service.ICommunityService;
import com.nari.baseinfo.service.IFileMappingService;
import com.nari.baseinfo.service.IHeatRoomService;
import com.nari.baseinfo.service.IProjectService;
import com.nari.coreframe.action.struts2.JSONActionSupport;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResultWithPageCond;
import com.nari.sink.bo.PSinkInfo;
import com.nari.sink.service.ISinkBiz;
import com.opensymphony.xwork2.Action;

public class SinkAction extends JSONActionSupport {
	private static final long serialVersionUID = 3738564934942292157L;
	private ISinkBiz iSinkBiz;
	private IProjectService iProjectService;
	private ICommunityService iCommunityService;
	private IBuildingService iBuildingService;
	private IHeatRoomService iHeatRoomService;
	private IFileMappingService iFileMappingService;
	//散热器信息查询
	private String sinkNo;
	private String projectUID;
	private String communityUID;
	private String buildingUID;
	private String heatRoomUID;
	private List<IProject> projectList;
	private List<ICommunity> communityList;
	private List<IBuilding> buildingList;
	private List<IHeatRoom> heatRoomList;
	//分页查询
	private int pageIndex = 1; //默认查询第一页列表
	private PageCond pageCond;
	private int pageSize;
	private ResultWithPageCond<PSinkInfo> heatSinkerPage;
	private Map<String, String> pageProjectMap;
	private Map<String, String> pageCommunityMap;
	private Map<String, String> pageBuildingMap;
	private Map<String, String> pageHeatRoomMap;
	//散热器资料管理
	private String heatSinkerUID;
	private PSinkInfo heatSinker;
	//安装照片附件
	private IFileMapping attachFileMap;
	private File attachFile;
	private String attachFileFileName;
	//散热器详细资料
	private IProject project;
	private ICommunity community;
	private IBuilding building;
	private IHeatRoom heatRoom;
	//散热器列表查询
	private List<PSinkInfo> sinkerList;
	
	/**
	 * 分页查询散热器列表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-28
	 */
	public String queryHeatSinkerPage() throws Exception {
		//所属项目列表
		this.projectList = this.iProjectService.queryAllProjectInOrder();
		if(this.projectList == null) {
			this.projectList = new ArrayList<IProject>();
		}
		//所属小区列表
		if(this.projectUID!=null && !"".equals(projectUID)) {
			this.communityList = this.iCommunityService.queryCommunitysByBelongings(null, null, null, projectUID);
		} else {
			this.communityList = new ArrayList<ICommunity>();
		}
		//所属楼栋列表
		this.buildingList = this.iBuildingService.queryBuildingByBelongings(projectUID, communityUID);
		//所属热用户
		this.heatRoomList = this.iHeatRoomService.queryHeatRoomsByBelongs(projectUID, communityUID, null, buildingUID);
		//分页信息
		this.pageCond = new PageCond();
		int start = (this.pageIndex - 1) * this.pageCond.getLimit();
		this.pageCond.setStart(start);
		//分页结果
		this.heatSinkerPage = this.iSinkBiz.queryHeatSinkPage(sinkNo, projectUID, communityUID, buildingUID, heatRoomUID, pageCond);
		//计算结果集的页数
		this.pageSize = new BigDecimal(this.heatSinkerPage.getPageCond().getTotal())
				.divide(new BigDecimal(this.heatSinkerPage.getPageCond().getLimit()), RoundingMode.UP).intValue();
		if(pageSize == 0) pageSize = 1;
//		//所属项目映射表
//		this.pageProjectMap = new HashMap<String, String>();
//		for(IProject project: this.projectList) {
//			this.pageProjectMap.put(project.getId(), project.getProjectName());
//		}
//		//所属小区映射表
//		this.pageCommunityMap = this.iSinkBiz.queryReferCommunityMap(heatSinkerPage.getList());
//		//所属楼栋映射表
//		this.pageBuildingMap = this.iSinkBiz.queryReferBuildingMap(heatSinkerPage.getList());
//		//所属热用户映射表
//		this.pageHeatRoomMap = this.iSinkBiz.queryReferHeatRoomMap(heatSinkerPage.getList());
		return "heatSinkerPage";
	}
	
	/**
	 * 散热器信息管理
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-28
	 */
	public String querySinkerForManage() throws Exception {
		if(this.heatSinkerUID!=null && !"".equals(heatSinkerUID)) {
			this.heatSinker = this.iSinkBiz.queryEntityById(heatSinkerUID);
			this.projectUID = this.heatSinker.getProjectID();
			this.communityUID = this.heatSinker.getCommunityID();
			this.buildingUID = this.heatSinker.getBuildingID();
			//散热器照片附件
			if(heatSinker.getPictureNo()!=null && !"".equals(heatSinker.getPictureNo().trim())) {
				this.attachFileMap = this.iFileMappingService.queryEntityById(heatSinker.getPictureNo());
			}
		}
		//所属项目列表
		this.projectList = this.iProjectService.queryAllProjectInOrder();
		if(this.projectList == null) {
			this.projectList = new ArrayList<IProject>();
		}
		//所属所属小区列表
		if(this.projectUID!=null && !"".equals(projectUID)) {
			this.communityList = this.iCommunityService.queryCommunitysByBelongings(null, null, null, projectUID);
		} else {
			this.communityList = new ArrayList<ICommunity>();
		}
		//所属楼栋列表
		this.buildingList = this.iBuildingService.queryBuildingByBelongings(projectUID, communityUID);
		//所属热用户列表
		this.heatRoomList = this.iHeatRoomService.queryHeatRoomsByBelongs(projectUID, communityUID, null, buildingUID);
		return "heatSinkerManage";
	}
	
	/**
	 * 新增或修改散热器信息
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-28
	 */
	public String saveOrUpdateHeatSinker() throws Exception {
		this.rmsg = this.iSinkBiz.saveOrUpdateHeatSinker(heatSinker, attachFile, attachFileFileName);
		this.writeResponseToFileAsyncSubmit(JSONUtil.serialize(rmsg));
		return Action.SUCCESS;
	}
	
	/**
	 * 异步删除散热器附件
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-11
	 */
	public String deleteHeatSinkerAttach() throws Exception {
		this.rmsg = this.iSinkBiz.deleteSinkerAttachFile(heatSinkerUID);
		this.writeJsonStrToResponse(JSONUtil.serialize(rmsg));
		return Action.SUCCESS;
	}
	
	/**
	 * 根据唯一标识删除散热器信息
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-28
	 */
	public String deleteHeatSinkerByUID() throws Exception {
		this.rmsg = this.iSinkBiz.deleteHeatSinkerByUID(heatSinkerUID);
		this.writeJsonStrToResponse(JSONUtil.serialize(rmsg));
		return Action.SUCCESS;
	}
	
	/**
	 * 散热器详细资料查询
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-28
	 */
	public String queryHeatSinkerDetail() throws Exception {
		if(this.heatSinkerUID!=null && !"".equals(heatSinkerUID)) {
			this.heatSinker = this.iSinkBiz.queryEntityById(heatSinkerUID);
			//所属项目
			this.project = this.iProjectService.queryEntityById(heatSinker.getProjectID());
			//所属小区
			this.community = this.iCommunityService.queryEntityById(heatSinker.getCommunityID());
			//所属楼栋
			this.building = this.iBuildingService.queryEntityById(heatSinker.getBuildingID());
			//所属热用户
			this.heatRoom = this.iHeatRoomService.queryEntityById(heatSinker.getRoomId());
			if(heatSinker.getPictureNo()!=null && !"".equals(heatSinker.getPictureNo())) {
				this.attachFileMap = this.iFileMappingService.queryEntityById(heatSinker.getPictureNo());
			}
		}
		return "sinkerDetail";
	}
	
	/**
	 * 根据所属关系查询散热器列表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-29
	 */
	public String querySinkersByBelongs() throws Exception {
		this.sinkerList = this.iSinkBiz.querySinkerListByBelongs(projectUID, communityUID, buildingUID, heatRoomUID);
		this.writeJsonStrToResponse(JSONUtil.serialize(sinkerList));
		return Action.SUCCESS;
	}
	
	/**
	 * 根据所属关系查询可与分配表绑定的散热器列表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-29
	 */
	public String queryBindableSinkersByBelongs() throws Exception {
		this.sinkerList = this.iSinkBiz.queryBindableSinkerByBelongs(projectUID, communityUID, buildingUID, heatRoomUID);
		this.writeJsonStrToResponse(JSONUtil.serialize(sinkerList));
		return Action.SUCCESS;
	}
	
	/**
	 * 通过热用户ID查询所属的分配表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-9-4
	 */
	public String querySinkerByHeatRoomUID() throws Exception {
		if(this.heatRoomUID!=null && !"".equals(heatRoomUID)) {
			this.heatRoom = this.iHeatRoomService.queryEntityById(heatRoomUID);
			//所属项目、小区、楼栋、热用户信息
			this.projectUID = this.heatRoom.getProjectID();
			this.communityUID = this.heatRoom.getCommunityID();
			this.buildingUID = this.heatRoom.getBuildingID();
		}
		//分页信息
		this.pageCond = new PageCond();
		int start = (this.pageIndex - 1) * this.pageCond.getLimit();
		this.pageCond.setStart(start);
		//分页结果
		this.heatSinkerPage = this.iSinkBiz.querySinkerPageByRoomUID(sinkNo, projectUID, communityUID, buildingUID, heatRoomUID, pageCond);
		//计算结果集的页数
		this.pageSize = new BigDecimal(this.heatSinkerPage.getPageCond().getTotal())
				.divide(new BigDecimal(this.heatSinkerPage.getPageCond().getLimit()), RoundingMode.UP).intValue();
		if(pageSize == 0) pageSize = 1;
		return "roomSinkerPage";
	}

	/**
	 * @return the iSinkBiz
	 */
	@JSON(serialize=false)
	public ISinkBiz getiSinkBiz() {
		return iSinkBiz;
	}

	/**
	 * @param iSinkBiz the iSinkBiz to set
	 */
	public void setiSinkBiz(ISinkBiz iSinkBiz) {
		this.iSinkBiz = iSinkBiz;
	}

	/**
	 * @return the iProjectService
	 */
	@JSON(serialize=false)
	public IProjectService getiProjectService() {
		return iProjectService;
	}

	/**
	 * @param iProjectService the iProjectService to set
	 */
	public void setiProjectService(IProjectService iProjectService) {
		this.iProjectService = iProjectService;
	}

	/**
	 * @return the iCommunityService
	 */
	@JSON(serialize=false)
	public ICommunityService getiCommunityService() {
		return iCommunityService;
	}

	/**
	 * @param iCommunityService the iCommunityService to set
	 */
	public void setiCommunityService(ICommunityService iCommunityService) {
		this.iCommunityService = iCommunityService;
	}

	/**
	 * @return the iBuildingService
	 */
	@JSON(serialize=false)
	public IBuildingService getiBuildingService() {
		return iBuildingService;
	}

	/**
	 * @param iBuildingService the iBuildingService to set
	 */
	public void setiBuildingService(IBuildingService iBuildingService) {
		this.iBuildingService = iBuildingService;
	}

	/**
	 * @return the iHeatRoomService
	 */
	@JSON(serialize=false)
	public IHeatRoomService getiHeatRoomService() {
		return iHeatRoomService;
	}

	/**
	 * @param iHeatRoomService the iHeatRoomService to set
	 */
	public void setiHeatRoomService(IHeatRoomService iHeatRoomService) {
		this.iHeatRoomService = iHeatRoomService;
	}

	/**
	 * @return the iFileMappingService
	 */
	@JSON(serialize=false)
	public IFileMappingService getiFileMappingService() {
		return iFileMappingService;
	}

	/**
	 * @param iFileMappingService the iFileMappingService to set
	 */
	public void setiFileMappingService(IFileMappingService iFileMappingService) {
		this.iFileMappingService = iFileMappingService;
	}

	/**
	 * @return the sinkNo
	 */
	public String getSinkNo() {
		return sinkNo;
	}

	/**
	 * @param sinkNo the sinkNo to set
	 */
	public void setSinkNo(String sinkNo) {
		this.sinkNo = sinkNo;
	}

	/**
	 * @return the projectUID
	 */
	public String getProjectUID() {
		return projectUID;
	}

	/**
	 * @param projectUID the projectUID to set
	 */
	public void setProjectUID(String projectUID) {
		this.projectUID = projectUID;
	}

	/**
	 * @return the communityUID
	 */
	public String getCommunityUID() {
		return communityUID;
	}

	/**
	 * @param communityUID the communityUID to set
	 */
	public void setCommunityUID(String communityUID) {
		this.communityUID = communityUID;
	}

	/**
	 * @return the buildingUID
	 */
	public String getBuildingUID() {
		return buildingUID;
	}

	/**
	 * @param buildingUID the buildingUID to set
	 */
	public void setBuildingUID(String buildingUID) {
		this.buildingUID = buildingUID;
	}

	/**
	 * @return the heatRoomUID
	 */
	public String getHeatRoomUID() {
		return heatRoomUID;
	}

	/**
	 * @param heatRoomUID the heatRoomUID to set
	 */
	public void setHeatRoomUID(String heatRoomUID) {
		this.heatRoomUID = heatRoomUID;
	}

	/**
	 * @return the projectList
	 */
	public List<IProject> getProjectList() {
		return projectList;
	}

	/**
	 * @param projectList the projectList to set
	 */
	public void setProjectList(List<IProject> projectList) {
		this.projectList = projectList;
	}

	/**
	 * @return the communityList
	 */
	public List<ICommunity> getCommunityList() {
		return communityList;
	}

	/**
	 * @param communityList the communityList to set
	 */
	public void setCommunityList(List<ICommunity> communityList) {
		this.communityList = communityList;
	}

	/**
	 * @return the buildingList
	 */
	public List<IBuilding> getBuildingList() {
		return buildingList;
	}

	/**
	 * @param buildingList the buildingList to set
	 */
	public void setBuildingList(List<IBuilding> buildingList) {
		this.buildingList = buildingList;
	}

	/**
	 * @return the heatRoomList
	 */
	public List<IHeatRoom> getHeatRoomList() {
		return heatRoomList;
	}

	/**
	 * @param heatRoomList the heatRoomList to set
	 */
	public void setHeatRoomList(List<IHeatRoom> heatRoomList) {
		this.heatRoomList = heatRoomList;
	}

	/**
	 * @return the pageIndex
	 */
	public int getPageIndex() {
		return pageIndex;
	}

	/**
	 * @param pageIndex the pageIndex to set
	 */
	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	/**
	 * @return the pageCond
	 */
	public PageCond getPageCond() {
		return pageCond;
	}

	/**
	 * @param pageCond the pageCond to set
	 */
	public void setPageCond(PageCond pageCond) {
		this.pageCond = pageCond;
	}

	/**
	 * @return the pageSize
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * @param pageSize the pageSize to set
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * @return the heatSinkerPage
	 */
	public ResultWithPageCond<PSinkInfo> getHeatSinkerPage() {
		return heatSinkerPage;
	}

	/**
	 * @param heatSinkerPage the heatSinkerPage to set
	 */
	public void setHeatSinkerPage(ResultWithPageCond<PSinkInfo> heatSinkerPage) {
		this.heatSinkerPage = heatSinkerPage;
	}

	/**
	 * @return the pageProjectMap
	 */
	public Map<String, String> getPageProjectMap() {
		return pageProjectMap;
	}

	/**
	 * @param pageProjectMap the pageProjectMap to set
	 */
	public void setPageProjectMap(Map<String, String> pageProjectMap) {
		this.pageProjectMap = pageProjectMap;
	}

	/**
	 * @return the pageCommunityMap
	 */
	public Map<String, String> getPageCommunityMap() {
		return pageCommunityMap;
	}

	/**
	 * @param pageCommunityMap the pageCommunityMap to set
	 */
	public void setPageCommunityMap(Map<String, String> pageCommunityMap) {
		this.pageCommunityMap = pageCommunityMap;
	}

	/**
	 * @return the pageBuildingMap
	 */
	public Map<String, String> getPageBuildingMap() {
		return pageBuildingMap;
	}

	/**
	 * @param pageBuildingMap the pageBuildingMap to set
	 */
	public void setPageBuildingMap(Map<String, String> pageBuildingMap) {
		this.pageBuildingMap = pageBuildingMap;
	}

	/**
	 * @return the pageHeatRoomMap
	 */
	public Map<String, String> getPageHeatRoomMap() {
		return pageHeatRoomMap;
	}

	/**
	 * @param pageHeatRoomMap the pageHeatRoomMap to set
	 */
	public void setPageHeatRoomMap(Map<String, String> pageHeatRoomMap) {
		this.pageHeatRoomMap = pageHeatRoomMap;
	}

	/**
	 * @return the heatSinkerUID
	 */
	public String getHeatSinkerUID() {
		return heatSinkerUID;
	}

	/**
	 * @param heatSinkerUID the heatSinkerUID to set
	 */
	public void setHeatSinkerUID(String heatSinkerUID) {
		this.heatSinkerUID = heatSinkerUID;
	}

	/**
	 * @return the heatSinker
	 */
	public PSinkInfo getHeatSinker() {
		return heatSinker;
	}

	/**
	 * @param heatSinker the heatSinker to set
	 */
	public void setHeatSinker(PSinkInfo heatSinker) {
		this.heatSinker = heatSinker;
	}

	/**
	 * @return the attachFileMap
	 */
	public IFileMapping getAttachFileMap() {
		return attachFileMap;
	}

	/**
	 * @param attachFileMap the attachFileMap to set
	 */
	public void setAttachFileMap(IFileMapping attachFileMap) {
		this.attachFileMap = attachFileMap;
	}

	/**
	 * @return the attachFile
	 */
	public File getAttachFile() {
		return attachFile;
	}

	/**
	 * @param attachFile the attachFile to set
	 */
	public void setAttachFile(File attachFile) {
		this.attachFile = attachFile;
	}

	/**
	 * @return the attachFileFileName
	 */
	public String getAttachFileFileName() {
		return attachFileFileName;
	}

	/**
	 * @param attachFileFileName the attachFileFileName to set
	 */
	public void setAttachFileFileName(String attachFileFileName) {
		this.attachFileFileName = attachFileFileName;
	}

	/**
	 * @return the project
	 */
	public IProject getProject() {
		return project;
	}

	/**
	 * @param project the project to set
	 */
	public void setProject(IProject project) {
		this.project = project;
	}

	/**
	 * @return the community
	 */
	public ICommunity getCommunity() {
		return community;
	}

	/**
	 * @param community the community to set
	 */
	public void setCommunity(ICommunity community) {
		this.community = community;
	}

	/**
	 * @return the building
	 */
	public IBuilding getBuilding() {
		return building;
	}

	/**
	 * @param building the building to set
	 */
	public void setBuilding(IBuilding building) {
		this.building = building;
	}

	/**
	 * @return the heatRoom
	 */
	public IHeatRoom getHeatRoom() {
		return heatRoom;
	}

	/**
	 * @param heatRoom the heatRoom to set
	 */
	public void setHeatRoom(IHeatRoom heatRoom) {
		this.heatRoom = heatRoom;
	}

	/**
	 * @return the sinkerList
	 */
	public List<PSinkInfo> getSinkerList() {
		return sinkerList;
	}

	/**
	 * @param sinkerList the sinkerList to set
	 */
	public void setSinkerList(List<PSinkInfo> sinkerList) {
		this.sinkerList = sinkerList;
	}

}
